/**
 * Video.m by Giraldo Rosales.
 * Visit www.liquidgear.net for documentation and updates.
 *
 * Copyright (c) 2009 Nitrogen Design, Inc. All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 **/
#if LG_USE_VIDEO

#import "Video.h"
#import "LiquidGearAppDelegate.h"

NSString * const OverlayViewTouchNotification = @"overlayViewTouch";

@implementation Video

#pragma mark Movie Player Routines

//  Notification called when the movie finished preloading.
- (void) moviePreloadDidFinish:(NSNotification*)notification {
	/* 
	 < add your code here >
	 */
}

//  Notification called when the movie finished playing.
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
	/*     
	 < add your code here >
	 
	 For example:
	 MPMoviePlayerController* theMovie=[aNotification object];
	 etc.
	 */
}

//  Notification called when the movie scaling mode has changed.
- (void) movieScalingModeDidChange:(NSNotification*)notification {
	/* 
	 < add your code here >
	 
	 For example:
	 MPMoviePlayerController* theMovie=[aNotification object];
	 etc.
	 */
}

-(void)initMoviePlayer {
	/*
	 Because it takes time to load movie files into memory, MPMoviePlayerController
	 automatically begins loading your movie file shortly after you initialize a new 
	 instance. When it is done preloading the movie file, it sends the
	 MPMoviePlayerContentPreloadDidFinishNotification notification to any registered 
	 observers. If an error occurred during loading, the userInfo dictionary of the 
	 notification object contains the error information. If you call the play method 
	 before preloading is complete, no notification is sent and your movie begins 
	 playing as soon as it is loaded into memory.
	 */
	
	// Register to receive a notification when the movie is in memory and ready to play.
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
	/*
	 
	 Now create a MPMoviePlayerController object using the movie file provided in our bundle.
	 
	 The MPMoviePlayerController class supports any movie or audio files that already play 
	 correctly on an iPod or iPhone. For movie files, this typically means files with the extensions 
	 .mov, .mp4, .mpv, and .3gp and using one of the following compression standards:
	 
	 - H.264 Baseline Profile Level 3.0 video, up to 640 x 480 at 30 fps. Note that B frames 
	 are not supported in the Baseline profile.
	 
	 - MPEG-4 Part 2 video (Simple Profile)
	 
	 If you use this class to play audio files, it displays a black screen while the audio plays. For 
	 audio files, this class class supports AAC-LC audio at up to 48 kHz.
	 
	 */
	
	mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[self movieURL]];
	
	/*
	 In addition to the MPMoviePlayerContentPreloadDidFinishNotification notification,
	 the MPMoviePlayerPlaybackDidFinishNotification notification is sent to
	 registered observers when the movie has finished playing, and the 
	 MPMoviePlayerScalingModeDidChangeNotification notification is sent when the 
	 movie scaling mode has changed.
	 */
	
	// Register to receive a notification when the movie has finished playing. 
	[[NSNotificationCenter defaultCenter] addObserver:self 
																					 selector:@selector(moviePlayBackDidFinish:) 
																							 name:MPMoviePlayerPlaybackDidFinishNotification 
																						 object:mMoviePlayer];
	
	// Register to receive a notification when the movie scaling mode has changed. 
	[[NSNotificationCenter defaultCenter] addObserver:self 
																					 selector:@selector(movieScalingModeDidChange:) 
																							 name:MPMoviePlayerScalingModeDidChangeNotification 
																						 object:mMoviePlayer];
	
	/* Set movie player settings (scaling, controller type and background color) to the currently set values
	 as specified in the Settings application */
	
	/* 
	 Movie scaling mode can be one of: MPMovieScalingModeNone, MPMovieScalingModeAspectFit,
	 MPMovieScalingModeAspectFill, MPMovieScalingModeFill.
   */
	//LiquidGearAppDelegate *appDelegate = (LiquidGearAppDelegate *)[[UIApplication sharedApplication] delegate];
	//mMoviePlayer.scalingMode = appDelegate.scalingMode;
	
	/* 
	 Movie control mode can be one of: MPMovieControlModeDefault, MPMovieControlModeVolumeOnly,
	 MPMovieControlModeHidden.
   */
	//mMoviePlayer.movieControlMode = appDelegate.controlMode;
	
	/*
	 The color of the background area behind the movie can be any UIColor value.
	 */
	//UIColor *colors[15] = {[UIColor blackColor], [UIColor darkGrayColor], [UIColor lightGrayColor], [UIColor whiteColor], 
	//	[UIColor grayColor], [UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor cyanColor], 
	//	[UIColor yellowColor], [UIColor magentaColor],[UIColor orangeColor], [UIColor purpleColor], [UIColor brownColor], 
	//[UIColor clearColor]};
	//mMoviePlayer.backgroundColor = colors[ [appDelegate backgroundColor] ];
	
	/*
	 Register as an observer of the "overlayViewTouch" notification.
	 
	 Any user touches to the MyOverlayView (not in the button) will
	 result in the "overlayViewTouch" notification being posted, and
	 the overlayViewTouches: method in this class will be called
	 */
	[[NSNotificationCenter defaultCenter] 
	 addObserver:self 
	 selector:@selector(overlayViewTouches:) 
	 name:OverlayViewTouchNotification 
	 object:nil];
}

-(IBAction)playMovie:(id)sender {
	/*
	 As soon as you call the play: method, the player initiates a transition that fades 
	 the screen from your current window content to the designated background 
	 color of the player. If the movie cannot begin playing immediately, the player 
	 object continues displaying the background color and may also display a progress 
	 indicator to let the user know the movie is loading. When playback finishes, the 
	 player uses another fade effect to transition back to your window content.
	 */
	
	[mMoviePlayer play];
	
	/*
	 Once the movie window has been created and started playing we will add our overlay
	 view as a subview and it will be displayed above the movie view. This will allow
	 us to display any controls on top of the movie as it plays.
	 */
	
	NSArray *windows = [[UIApplication sharedApplication] windows];
	// Locate the movie player window
	UIWindow *moviePlayerWindow = [windows objectAtIndex:1];
	// Add our overlay view to the movie player's subviews so it’s 
	// displayed above it.
	[moviePlayerWindow addSubview:controls];
}

#pragma mark View Controller Routines

// return a URL for the movie file in our bundle
-(NSURL *)movieURL {
	if (mMovieURL == nil) {
		NSBundle *bundle = [NSBundle mainBundle];
		
		if (bundle) {
			NSString *moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"];
			
			if (moviePath) {
				mMovieURL = [NSURL fileURLWithPath:moviePath];
				[mMovieURL retain];
			}
		}
	}
	
	return mMovieURL;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return YES;
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Low on memory: Release anything that's not essential, such as cached data, or perhaps 
	// unload the movie, etc.
}

// Touches in the MyOverlayView (not in the overlay button)
// post the "overlayViewTouch" notification and will send
// the overlayViewTouches: message
- (void)overlayViewTouches:(NSNotification *)notification {
	// Handle touches to the MyOverlayView here... 
}

// Action method for the MyOverlayView button
-(IBAction)overlayViewButtonPress:(id)sender {
	// Handle touches to the MyOverlayView 
	// button here... 
}

- (void)dealloc {
	[mMovieURL release];
	
	// remove movie notifications
	[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:mMoviePlayer];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerScalingModeDidChangeNotification object:mMoviePlayer];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:OverlayViewTouchNotification object:mMoviePlayer];
	
	// free our movie player
	[mMoviePlayer release];
	[super dealloc];
}

@end
#endif